//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Configuration;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.Blogs
{
	/// <summary>
	/// Summary description for CategoryForm.
	/// </summary>
	public class LinkForm : BaseBlogPage
	{
        protected TextBox LinkTitle;
        protected FormLabel FormLabel1;
        protected TextBox LinkUrl;
		protected TextBox LinkDesc;
		protected TextBox LinkRel;
		protected DropDownList RelQuickList;
        protected CommunityServer.Controls.Style ControlPanelStyle;
        protected FormLabel tt;
        protected RequiredFieldValidator LinkTitleValidator;
        protected CheckBox CategoryEnabled;
		protected RequiredFieldValidator LinkUrlValidator;
		protected ResourceLinkButton SaveButton;
	
		override protected void OnInit(EventArgs e)
		{
			this.SaveButton.Click += new EventHandler(this.SaveButton_Click);
			this.Load += new EventHandler(this.Page_Load);

			LinkTitleValidator.Text = Components.ResourceManager.GetString("CP_Blog_LinkForm_TitleValidate");
			LinkUrlValidator.Text = Components.ResourceManager.GetString("CP_Blog_LinkForm_UrlValidate");

			base.OnInit(e);
		}

		private void Page_Load(object sender, EventArgs e)
		{
            if(!IsPostBack)
            {
            	Link l = GetLink();	        
                LinkTitle.Text = l.Title;
                LinkUrl.Text = l.Url;
				LinkDesc.Text = l.Description;
				LinkRel.Text = l.Rel;
                CategoryEnabled.Checked = l.IsEnabled;
            }

			RelQuickList.Attributes.Add("onchange", "insertRel();");
			LoadRelQuickList();

			Head.AddTitle(Components.ResourceManager.GetString("CP_Blog_LinkForm_Title"), this.Context);
		}

        protected Link GetLink()
        {
            LinkCategory lc = LinkCategories.GetCategory(Int32.Parse(Request.QueryString["CategoryID"]),this.CurrentWeblog.SectionID,true);
            if(lc == null)
                throw new Exception("Category does not exist");
            

            Link l = null;

            if(Request.QueryString["LinkID"] != null && Request.QueryString["LinkID"] != "-1")
            {
                
                l = Links.GetLink(Int32.Parse(Request.QueryString["LinkID"]),lc.LinkCategoryID,true);
                if(l == null)
                    throw new Exception("Link could not be found");
            }
            else
            {
                l = new Link();
                l.LinkCategoryID = lc.LinkCategoryID;
				l.IsEnabled = true;
				l.Url = "http://";
            }

            return l;
        }


		private void SaveButton_Click(object sender, EventArgs e)
		{
            if(Page.IsValid)
            {
                Link l = GetLink();
                l.Title = LinkTitle.Text;
                l.Url = LinkUrl.Text;
                l.IsEnabled = CategoryEnabled.Checked;
				l.Description = LinkDesc.Text;
				l.Rel = LinkRel.Text;

                if(l.LinkID > 0)
                    Links.UpdateLink(l);
                else
                    Links.CreateLink(l);

				Modal.ClosePage(Page,"true") ;
            }
			
		}

		private void InitializeComponent()
		{
		
		}

		private void LoadRelQuickList()
		{
			
			RelQuickList.Items.Add(new ListItem(ResourceManager.GetString("CP_Blog_LinkForm_CommonRelations"), ""));

			string userLanguage = CSContext.Current.User.Profile.Language;
			if(userLanguage == "")
				userLanguage = CSConfiguration.GetConfig().DefaultLanguage;

			Hashtable Relations = Links.GetLinkRelValues(userLanguage);

			if (Relations.Count > 0)
			{
				foreach (string rel in Relations.Keys)
				{
					RelQuickList.Items.Add(new ListItem(Convert.ToString(Relations[rel]), rel));
				}
			}
			
		}
	}
}
